import React from 'react'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import _ from 'loadsh'
import { Form, DatePicker } from 'antd'
import { Line, Cell, Split } from '@components/ApprovePre/Approve'
import SelectSecretary from '../SelectSecretary'

const { Item } = Form

class DocketDatetimeInput extends Component {
  state = {
    secretarySelectorVisible: false,
    secretaryid: '',
    keyTitleMap: {}
  }

  initKeyTitleMap () {
    const map = {}
    const list = _.get(this.props.instance || {}, 'currentStep.vars', [])
    list.forEach(li => {
      map[li.key] = li.title
    })
    this.setState({
      keyTitleMap: map
    })
  }

  componentDidUpdate (prev) {
    if (this.props.instance !== prev.instance) {
      this.initKeyTitleMap()
    }
  }

  componentDidMount () {
    this.initKeyTitleMap()
  }

  render () {
    const { keyTitleMap } = this.state
    const { id } = this.props.caseDetail
    const { secretarySelectorVisible, secretaryid } = this.state
    const setSecretary = (o) => {
      const { employee, employeeid } = o
      this.props.formRef.current.setFieldsValue({
        secretary: employee,
        vars: {
          processer: employee,
          processerid: employeeid
        }
      })
      this.setState({
        secretaryid: employeeid
      })
    }
    return (
      <div>
        <SelectSecretary caseid={id} visible={secretarySelectorVisible} value={secretaryid} hide={() => this.setState({ secretarySelectorVisible: false })} changeSecretary={o => setSecretary(o)} />
        <Line>
          <Cell>
            <Item label='操作者'>
              <span>{this.props.userInfo.realname}</span>
            </Item>
          </Cell>
          <Split />
          <Cell>
            <Item name={['vars', 'docket_datetime']} label={keyTitleMap.docket_datetime} rules={[{ required: true, message: `${keyTitleMap.docket_datetime}不能为空` }]}>
              <DatePicker placeholder={`请选择${keyTitleMap.docket_datetime}`} style={{ width: '100%' }} />
            </Item>
          </Cell>
        </Line>
      </div>
    )
  }
}

DocketDatetimeInput.proptypes = {
  caseDetail: PropTypes.object,
  userInfo: PropTypes.object,
  instance: PropTypes.object,
  formRef: PropTypes.object
}

DocketDatetimeInput.defaultProps = {
  caseDetail: {},
  userInfo: {},
  instance: {},
  formRef: {}
}

const mapState = mapStateToProps(function (state) {
  return {
    userInfo: state.basic.userInfo
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {

  }
})

export default connect(mapState, mapDispatch)(DocketDatetimeInput)
